<?php

use Biz\service\springHomeDecActivity\LuckyCharmTicketService;
use Biz\service\springHomeDecActivity\Util;

class ExchangeLuckyTicketAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_REGISTER;


    const VALIDATOR_TOAST_DEBUG = true;

    //参数校验-规则
    public const VALIDATOR_RULES = [
        'exchange_count' => 'require|integer',
    ];

    //参数校验-自定义消息
    public const VALIDATOR_MSG = [
        'exchange_count.require' => '参数错误',
    ];

    public function main()
    {
        $lockKey = 'spring_home_dec_act:exchange_lucky_ticket:' . $this->uid;
        Util::lockSubmit($lockKey);

        $exchange_count = $this->params['exchange_count'] ?? 0;
        if ($exchange_count <= 0) {
            throw new \Exception("参数错误", self::CODE_ERROR_VIEW_TOSTA);
        }
        $result = LuckyCharmTicketService::exchange($this->uid, $exchange_count);
        if (!$result) {
            throw new \Exception("兑换锦鲤券失败，请稍后重试", self::CODE_ERROR_VIEW_TOSTA);
        }
        $this->setSuccess('success');
    }
}